package bu.met.cs664;

/**
 * A game board, i.e. rooms of the game.
 */
public class Board {
	
	String[][] spaces;
	
	/**
	 * Constructor
	 * 
	 * @param width		the number of horizontal spaces
	 * @param height	the number of vertical spaces
	 */
	public Board(int width, int height) {
		spaces = new String[width][height];
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				spaces[x][y] = new String("");
			}
		}
	}
	
	/**
	 * Constructor
	 * 
	 * @param boardDesc	State of each square of the board
	 */
	public Board(String[][] boardDesc) {
		spaces = boardDesc;
	}
	
	/**
	 * Is the position within the game board?
	 * 
	 * @param move	An array of the potential move coordinates
	 * 
	 * @return	true if the move is on the game board, false otherwise
	 */
	public boolean isValid(int[] move) {
		return ((move[0] >= 0) && (move[0] < spaces.length) && (move[1] >= 0) && (move[1] < spaces[0].length));
	}
	
	/**
	 * Pick up the gold if it is in this square.
	 * 
	 * @param move	An array of the move coordinates
	 * 
	 * @return	true if the square had gold, false otherwise
	 */
	public boolean pickUpGold(int[] move) {
		boolean hasGold = false;
		
		if (spaces[move[0]][move[1]].contains("G")) {
			spaces[move[0]][move[1]] = spaces[move[0]][move[1]].replace('G', 'X');
			hasGold = true;
		}
		return hasGold;
	}
	
	/**
	 * What is the most bottom/right position?
	 * 
	 * @return An array of the coordinates
	 */
	public int[] getMaxPosition() {
		return new int[]{(spaces.length - 1), (spaces[0].length - 1)};
	}
	
	public String getState(int[] position) {
		return spaces[position[0]][position[1]];
	}
	
	/**
	 * Display the game board with the player.
	 * 
	 * @param move	The current position of the player
	 */
	public void print(int[] move) {

		final int SpaceWidth = 4;
		final String SpacePaddingString = " ";
		final String PlayerString = "*";
		
		String verticalSeparator = " | ";
		String horizontalSeparator = "\n------";
		
		for (int i = 1; i < spaces.length; i++) {
			horizontalSeparator = horizontalSeparator.concat("+------");
		}
		horizontalSeparator = horizontalSeparator.concat("\n");
		
		StringBuilder boardBuilder = new StringBuilder();
		for (int y = 0; y < spaces[0].length; y++) {
			if (y != 0) {
				boardBuilder.append(horizontalSeparator);
			}
			
			boardBuilder.append(" ");
			
			for (int x = 0; x < spaces.length; x++) {
				if (x != 0) {
					boardBuilder.append(verticalSeparator);
				}
				String space = "";
				
				if ((x == move[0]) && (y == move[1])) {
					space = space.concat(PlayerString);
				}
				space = space.concat(spaces[x][y]);
				
				while (space.length() < SpaceWidth) {
					space = space.concat(SpacePaddingString);
				}
				
				boardBuilder.append(space);
			}
		}
		
		System.out.println(boardBuilder.toString());
	}
	
	boolean shoot(int[] pos, Direction dir) {
		int[] path = pos.clone();
		boolean hitWumpus = false;
		
		while (isValid(path)) {
			
			if (spaces[path[0]][path[1]].contains("W")) {
				spaces[path[0]][path[1]] = spaces[path[0]][path[1]].replace('W', 'X');
				hitWumpus = true;
			}
			
			switch (dir) {
				case NORTH:
					path[1]--;
					break;
				case WEST:
					path[0]--;
					break;
				case SOUTH:
					path[1]++;
					break;
				case EAST:
					path[0]++;
					break;
			}
		}
		
		return hitWumpus;
	}
}
